<?php
/**
 * Created by PhpStorm
 * User: 龍zero
 * Date: 2022-04-10
 * Time: 16:28
 */
declare(strict_types=1);

namespace App\Listener;

use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Hyperf\Validation\Event\ValidatorFactoryResolved;

/**
 * @Listener
 */
class ValidatorFactoryResolvedListener implements ListenerInterface
{

    public function listen(): array
    {
        return [
            ValidatorFactoryResolved::class,
        ];
    }

    public function process(object $event):void
    {
        /**  @var ValidatorFactoryInterface $validatorFactory */
        $validatorFactory = $event->validatorFactory;
        #region 注册positive验证器-正整数
        $validatorFactory->extend('positive', function ($attribute, $value, $parameters, $validator) {
            return $this->isPostiveInteger($value);
        });
        // 当创建一个自定义验证规则时，你可能有时候需要为错误信息定义自定义占位符这里扩展了 :foo 占位符
        $validatorFactory->replacer('positive', function ($message, $attribute, $rule, $parameters) {
            $message = ':positive 必须是一个正整数';
            return str_replace(':positive', $attribute, $message);
        });
        #endregion

        #region 注册isMobile验证器-手机号
        $validatorFactory->extend('isMobile', function ($attribute, $value, $parameters, $validator) {
            return $this->isMobile($value);
        });
        // 当创建一个自定义验证规则时，你可能有时候需要为错误信息定义自定义占位符这里扩展了 :foo 占位符
        $validatorFactory->replacer('isMobile', function ($message, $attribute, $rule, $parameters) {
            $message = ':isMobile不是一个正确的手机号码';
            return str_replace(':isMobile', $attribute, $message);
        });
        #endregion

        #region 注册checkIDs-检测逗号隔开的ID字符串
        $validatorFactory->extend('checkIDs', function ($attribute, $value, $parameters, $validator) {
            $value = explode(',',$value);
            if(empty($value)){
                return false;
            }else{
                foreach($value as $id){
                    //传入如果有值判断是否是正整数
                    if(!$this->isPostiveInteger($id)){
                        return false;
                    }
                }
                return true;
            }
        });
        // 当创建一个自定义验证规则时，你可能有时候需要为错误信息定义自定义占位符这里扩展了 :foo 占位符
        $validatorFactory->replacer('checkIDs', function ($message, $attribute, $rule, $parameters) {
            $message = ':checkIDs ids必须是以逗号分隔的正整数';
            return str_replace(':checkIDs', $attribute, $message);
        });
        #endregion
    }

    private function isPostiveInteger($value){
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        }
        return false;
    }

    private function isMobile($value){
        $rule = '^1(3|4|5|6|7|8|9)[0-9]\d{8}$^';
        $res = preg_match($rule, $value);
        if (!$res) {
            return false;
        }
        return true;
    }

}
